<?php

/**
 * Vote actions.
 *
 * @package    ieasy
 * @subpackage Vote
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class voteActions extends sfActions
{
    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function connstr()
    {
        $server = "172.16.0.58";
        //$server="192.168.1.230";
        // $server=".";
        $username = 'sa';
        $password = '123456';
        $database = 'iEasy_new';
        $connstr = "Driver={SQL Server Native Client 10.0};Server=localhost;Database=$database";
        $dbconn = odbc_connect($connstr, $username, $password, SQL_CUR_USE_ODBC);
        if (!$dbconn) {
            die('Could not connect: ');
        }
        else return $dbconn;
    }

    public function executeAddvote($request)
    {
        if (!empty($_POST["celebrity_id"])) {

            if (!empty($_POST["celebrity_id"])) {
                $sql2 = "update Vote set count_i+=1,daily_count+=1,weekly_count+=1,monthly_count+=1 where celebrity_id=" . $_POST["celebrity_id"];
                $connection = Doctrine_Manager::connection();
                $STH = $connection->execute($sql2);
                if ($STH) {
                    $this->iscomment = 1;
                    echo "已经投票!";
                }
            }

        }
    }

    public function executeList()
    {
        $connection = Doctrine_Manager::connection();

        $sql2 = "SELECT  count_i,cast(name as varchar(100))name,cast(snapshot_big as varchar(100))snapshot_big,Celebrity.celebrity_id from Vote, Celebrity where Vote.celebrity_id=Celebrity.celebrity_id ";

        if (!empty($_GET["link"])) {
            $sql2 .= " where title like '" . $_GET["link"] . "%'";
        }

        $this->arr2 = array();
        $STH = $connection->execute($sql2);
        $STH->setFetchMode(PDO::FETCH_ASSOC);
        while ($row = $STH->fetch()) {

            $currentNews = array("celebrity_id" => $row["celebrity_id"], "snapshot_big" => $row["snapshot_big"], "name" => $row["name"], "count_i" => $row["count_i"]);
            $this->arr2[] = $currentNews;

        }


    }

    public function executeResult()
    {
        $connection = Doctrine_Manager::connection();
        //execute the SQL query and return records


        $sql_sum = "SELECT sum(count_i) as amount from Vote ";
        $STH = $connection->execute($sql_sum);
        $STH->setFetchMode(PDO::FETCH_ASSOC);
        $row = $STH->fetch();
        $amount = $row['amount'];

        $sql2 = "SELECT  cast(name as varchar(100))name,count_i,Celebrity.celebrity_id from Vote, Celebrity where Vote.celebrity_id=Celebrity.celebrity_id order by count_i desc";
        if (!empty($_GET["link"])) {
            $sql2 .= " where title like '" . $_GET["link"] . "%'";
        }

        $this->arr2 = array();
        $STH = $connection->execute($sql2);
        $STH->setFetchMode(PDO::FETCH_ASSOC);
        while ($row = $STH->fetch()) {

            $ratio = number_format($row['count_i'] / $amount * 100, 2);
            $ratiolab = $ratio / 100 * 100;
            $currentNews = array("celebrity_id" => $row["celebrity_id"], "ratio" => $ratio, "ratiolab" => $ratiolab, "name" => $row["name"], "count_i" => $row["count_i"]);
            $this->arr2[] = $currentNews;

        }


    }

}
